package com.xm.xmapp;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Apply_big_customerRequest;
import com.xm.xmapp.tools.Helper_SharedPreferences;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;

public class CompanyUserActivity extends BaseActivity implements OnClickListener {
	private TextView title, info;
	private ImageView back;
	private EditText text;
	private ImageView shenqing;
	private Apply_big_customerRequest requestBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_companyuser);
	}

	@Override
	public void initView() {

		title = (TextView) findViewById(R.id.title);
		shenqing = (ImageView) findViewById(R.id.shenqing);
		back = (ImageView) findViewById(R.id.back);
		text = (EditText) findViewById(R.id.text);
		info = (TextView) findViewById(R.id.info);
	}

	/**
	 * 
	 * 1极光推送，被邀请
	 */
	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case Type.A_CompanyUser.JPUSH_IN:
			finish();
			break;

		default:
			break;
		}
		return super.dataChang(flag, data);
	}

	@Override
	public void initData() {

		title.setText("企业用车");
		requestBean = new Apply_big_customerRequest();
		String str = Helper_SharedPreferences.get_str_sp(CodeConstant.Info + MyAppcation.getMyAppcation().getUid());
		if (str != null) {
			String str_text = "您申请的" + str + "已提交\n请等待客服人员与您联系";
			SpannableStringBuilder style = new SpannableStringBuilder(str_text);
			style.setSpan(new ForegroundColorSpan(Color.RED), 4, 4 + str.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			info.setText(style);
		}
	}

	@Override
	public void initEvent() {

		back.setOnClickListener(this);
		shenqing.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.shenqing:
			if (!text.getText().toString().equals("")
					&& StringTools.stringFilter(text.getText().toString()).equals(text.getText().toString())) {
				if (requestBean == null)
					requestBean = new Apply_big_customerRequest();
				requestBean.big_customer_name = text.getText().toString();
				apply_big_customer();
			} else {
				Tools.showMessage("不能输入非法字符！");
			}

			break;

		default:
			break;
		}
	}

	private void apply_big_customer() {

		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				String str = text.getText().toString().trim();
				Helper_SharedPreferences.set_str_sp(CodeConstant.Info + MyAppcation.getMyAppcation().getUid(), str);

				String str_text = "您申请的" + str + "已提交\n请等待客服人员与您联系";
				SpannableStringBuilder style = new SpannableStringBuilder(str_text);
				style.setSpan(new ForegroundColorSpan(Color.RED), 4, 4 + str.length(),
						Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
				info.setText(style);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				promptDialog.dismiss();
			}
		});
	}

}
